package org.algorithm.熊猫.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;

/**
 * @Date 2022/2/28
 * @Desc {https://leetcode-cn.com/problems/odd-even-linked-list/submissions/}
 */
public class 奇偶链表 {

    @Test
    public void test() {
        ListNode head = ListNode.build(1, 2, 3, 4, 5);
        System.out.println(ListNode.print(oddEvenList(head)));
    }

    public ListNode oddEvenList(ListNode head) {
        ListNode oddNodeDy = new ListNode();
        ListNode evenNodeDy = new ListNode();

        ListNode curtOddNode = oddNodeDy;
        ListNode curtEvenNode = evenNodeDy;

        while (head != null) {
            ListNode next = head.next;
            curtOddNode.next = head;
            curtOddNode = head;

            if (next != null) {
                curtEvenNode.next = next;
                curtEvenNode = next;
                head = next.next;
            } else {
                curtEvenNode.next = null;
                break;
            }
        }

        curtOddNode.next = evenNodeDy.next;
        return oddNodeDy.next;
    }
}
